package com.handpay.nocardpay.baselibrary.qualificationinfo.view.fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.handpay.nocardpay.baselibrary.R;
import com.handpay.nocardpay.baselibrary.commbase.BaseProtocolKey;
import com.handpay.nocardpay.baselibrary.commbase.view.fragment.BaseFragment;
import com.handpay.nocardpay.baselibrary.config.NoCardPayConfig;
import com.handpay.nocardpay.baselibrary.config.SecureManager;
import com.handpay.nocardpay.baselibrary.logutils.HPLog;
import com.handpay.nocardpay.baselibrary.permission.PermissionCode;
import com.handpay.nocardpay.baselibrary.permission.PermissionRequest;
import com.handpay.nocardpay.baselibrary.qualificationinfo.comm.CommonHelper;
import com.handpay.nocardpay.baselibrary.qualificationinfo.model.bean.BankList;
import com.handpay.nocardpay.baselibrary.qualificationinfo.model.bean.BundleBean;
import com.handpay.nocardpay.baselibrary.qualificationinfo.model.bean.QualificationQueryBean;
import com.handpay.nocardpay.baselibrary.qualificationinfo.model.bean.ResultSingleBankBean;
import com.handpay.nocardpay.baselibrary.qualificationinfo.model.bean.SubBankBean;
import com.handpay.nocardpay.baselibrary.qualificationinfo.model.bean.ThreeElementResult;
import com.handpay.nocardpay.baselibrary.qualificationinfo.presenter.BankPresenterImpl;
import com.handpay.nocardpay.baselibrary.qualificationinfo.presenter.QualificationPresenterImpl;
import com.handpay.nocardpay.baselibrary.qualificationinfo.view.IQualificationStatusView;
import com.handpay.nocardpay.baselibrary.qualificationinfo.view.IQualificationView;
import com.handpay.nocardpay.baselibrary.qualificationinfo.widgets.HPSubBankEditView;
import com.handpay.nocardpay.baselibrary.qualificationinfo.widgets.QualificationEditView;
import com.handpay.nocardpay.baselibrary.utils.AccountUtils;
import com.handpay.nocardpay.baselibrary.utils.ClientEngine;
import com.handpay.nocardpay.baselibrary.utils.CommonUtils;
import com.handpay.nocardpay.baselibrary.utils.DialogUtils;
import com.handpay.nocardpay.baselibrary.widget.BankCardNoEditTextView;
import com.xys.libzxing.zxing.activity.CaptureActivity;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;
import java.lang.ref.WeakReference;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by fplei on 2017/8/11.
 * 填写基本信息--基本信息
 */
public class QualificationBaseInfoFragment extends BaseFragment implements
        IQualificationView, IQualificationStatusView, HPSubBankEditView.OnBankLisenter, View.OnClickListener, BankCardNoEditTextView.BankCardInputListener, PermissionRequest.onPermissionRequestCallBack {

    private Toolbar mToolbar;
    //审核失败显示原因
    private LinearLayout reupload_warning;
    private TextView tv_failDes;
    //银行卡相关model
    private BankPresenterImpl bankModel = null;
    //资质信息相关model
    private QualificationPresenterImpl qualificationPresenter = null;
    private QualificationEditView releaName, certNo, shareCode;
    private ImageButton shareCamera;
    private EditText card_no;
    private HPSubBankEditView sub_bank;
    private LinearLayout linear_subbank;
    public BankList.ListBean mMainBank = new BankList.ListBean();
    public SubBankBean.ListBean mSubSelectedBank = new SubBankBean.ListBean();
    private TextView bank_name;
    //支行查询一页条数
    public static String PAGESIZE = "30";
    //处理自动查询支行消息
    private final static int HANDLER_MSG_TYPE_QUERY_SUBBANKLIST = 1001;
    private RefreshHandler refreshHandler;
    //滚轮
    private OptionsPickerView bankPickerView;
    //滚轮显示的数据
    private List<String> bankNames;
    //发卡行列表
    private BankList mBankList = new BankList();
    //资质页面传输的数据
    private BundleBean bundleBean;
    //权限操作
    private PermissionRequest permissonRequest;
    //权限列表
    private String[] permissons = Permission.CAMERA;

    private BankCardNoEditTextView relaBankCard;
    //是否第一次提交
    volatile private boolean isFirstSubmit=true;
    private int TAKE_PHOTO=100;
    private class RefreshHandler extends Handler {
        private WeakReference<QualificationBaseInfoFragment> mfragment;

        private RefreshHandler(QualificationBaseInfoFragment fragment) {
            this.mfragment = new WeakReference<QualificationBaseInfoFragment>(fragment);
        }

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case HANDLER_MSG_TYPE_QUERY_SUBBANKLIST: {
                    String s = (String) msg.obj;
                    if (null != s) {
                        s = s.trim();
                        QualificationBaseInfoFragment fragment = mfragment.get();
                        if (null != fragment && !TextUtils.isEmpty(s)) {
                            fragment.querySubBankList(fragment.mMainBank.getBankCode(), s);
                        }
                    }
                    break;
                }
                default:
                    break;
            }
        }
    }

    public static QualificationBaseInfoFragment newInstance() {
        Bundle args = new Bundle();
        QualificationBaseInfoFragment fragment = new QualificationBaseInfoFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public static QualificationBaseInfoFragment newInstance(Bundle bundle) {
        Bundle args = bundle;
        QualificationBaseInfoFragment fragment = new QualificationBaseInfoFragment();
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public int getLayoutId() {
        return R.layout.fragment_qualificationinfo;
    }

    @Override
    public void initView(View view) {
        mToolbar = (Toolbar) view.findViewById(R.id.toolbar);
        initToolbar(mToolbar, true);
        bundleBean = (BundleBean) getArguments().getParcelable(BaseProtocolKey.KEY_BASEINFO);
        reupload_warning = (LinearLayout) view.findViewById(R.id.reupload_warning);
        tv_failDes = (TextView) view.findViewById(R.id.tv_failDes);
        releaName = (QualificationEditView) view.findViewById(R.id.releaName);
        certNo = (QualificationEditView) view.findViewById(R.id.certNo);
        shareCode = (QualificationEditView) view.findViewById(R.id.shareCode);
        shareCamera = (ImageButton) view.findViewById(R.id.shareCamera);
        shareCamera.setOnClickListener(this);
        relaBankCard = (BankCardNoEditTextView) view.findViewById(R.id.relaBankCard);
        relaBankCard.setImeOptions(EditorInfo.IME_ACTION_DONE);
        card_no = (EditText) view.findViewById(R.id.bank_card_no_et);
        bank_name = (TextView) view.findViewById(R.id.bank_name);
        relaBankCard.setBankCardInputListener(this);
        linear_subbank = (LinearLayout) view.findViewById(R.id.linear_subbank);
        sub_bank = (HPSubBankEditView) view.findViewById(R.id.sub_bank);
        sub_bank.setLisenter(this);
        sub_bank.addTextChangedListener(subBankTextWatch);
        refreshHandler = new RefreshHandler(this);
        Button nextButton = (Button) view.findViewById(R.id.nextButton);
        nextButton.setOnClickListener(this);

        bankPickerView = new OptionsPickerView.Builder(_mActivity, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                BankList.ListBean bank = mBankList.getList().get(options1);
                sub_bank.setText("");
                sub_bank.clearList();
                mMainBank = bank;
                String banName = bank.getBankName();
                String bankSubFlag = bank.getSubBankFlag();
                if (!TextUtils.isEmpty(banName)) {
                    bank_name.setText(banName);
                }
                linearLayoutStatus(bank.getSubBankFlag());
            }
        }).build();
        fillContent(bundleBean);
    }

    //自动填入内容
    private void fillContent(BundleBean bundleBean) {
        //如果是第二次提交，推荐人,姓名,身份证将不能修改
        if (bundleBean != null) {
            if (!TextUtils.isEmpty(bundleBean.getAuditInfo()) || !TextUtils.isEmpty(AccountUtils.getAccountFailDes(_mActivity))) {
                reupload_warning.setVisibility(View.VISIBLE);
                tv_failDes.setText(AccountUtils.getAccountFailDes(_mActivity));
            }
            releaName.setText(bundleBean.getName());
            releaName.setCanNotUpdate();

            certNo.setText(bundleBean.getCertNo());
            certNo.setCanNotUpdate();

            shareCamera.setVisibility(View.INVISIBLE);
            HPLog.d(HPLog.SSX_TAG, bundleBean.toString());
            HPLog.d(HPLog.SSX_TAG, bundleBean.getShareCode());
            shareCode.setText(bundleBean.getShareCode());
            shareCode.setCanNotUpdate();

            relaBankCard.setText(bundleBean.getCardNo());
            bank_name.setText(bundleBean.getBankName());
            if (!TextUtils.isEmpty(bundleBean.getSubBankName())) {
                linear_subbank.setVisibility(View.VISIBLE);
                sub_bank.setText(bundleBean.getSubBankName());
            }
            sub_bank.setTag(bundleBean.getSubBankCode());
            isFirstSubmit=false;
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.shareCamera) {
            permissonRequest.requestPermission(permissons);
        } else if (v.getId() == R.id.nextButton) {
            checkCardThreeElements();
        }
    }

    private TextWatcher subBankTextWatch = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            HPLog.d(HPLog.SSX_TAG, "beforeTextChanged");
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            HPLog.d(HPLog.SSX_TAG, "onTextChanged");
        }

        @Override
        public void afterTextChanged(final Editable s) {
            if (mMainBank != null) {
                HPLog.i(HPLog.LFP_TAG, "mMainBank:" + mMainBank.toString());
            }
            if ("1".equals(mMainBank.getSubBankFlag())
                    && (null == mMainBank || "-1".equals(mMainBank.getBankCode()))) {
                String title = _mActivity.getString(R.string.tip);
                String message = _mActivity.getString(R.string.select_bank_tip);
                DialogUtils.showAlertDialog(_mActivity, title, message, true);
            } else {
                refreshHandler.removeMessages(HANDLER_MSG_TYPE_QUERY_SUBBANKLIST);
                Message msg = refreshHandler.obtainMessage();
                msg.what = HANDLER_MSG_TYPE_QUERY_SUBBANKLIST;
                msg.obj = s.toString();
                refreshHandler.sendMessageDelayed(msg, 300);
            }
        }
    };


    @Override
    public void initListener() {
        bank_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bankPickerView.show();
            }
        });
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        bankModel = new BankPresenterImpl(_mActivity, this);
        qualificationPresenter = new QualificationPresenterImpl(_mActivity, this, this);
        bankNames = new ArrayList<>();
        queryMainBankList();
        permissonRequest = new PermissionRequest(_mActivity, this);
    }

    //发起资质信息查询，如果是重新提交
    private void queryHasSubmitData() {
        if (AccountUtils.getAccountStatus(_mActivity) != AccountUtils.AccountStatus.NOUPLOAD) {
            if (AccountUtils.getAccountPhone(_mActivity) == null){ return;}
            HashMap<String, Object> params = new HashMap<>();
            params.put(BaseProtocolKey.ACCOUNT, SecureManager.getInstance().des(AccountUtils.getAccountName(_mActivity), 1, null));
            params.put(BaseProtocolKey.CHANNEL, ClientEngine.getInstance().CHANNEL);
            params.put(BaseProtocolKey.NEWBANKFLAG, "1");
            qualificationPresenter.queryQualificationInfo(params, true);
        }
    }

    /**
     * 查询主行
     */
    private void queryMainBankList() {
        HashMap<String, Object> p = new HashMap<String, Object>();
        p.put(BaseProtocolKey.CHANNEL, ClientEngine.getInstance().CHANNEL);
        bankModel.queryBankList(p, true);
    }

    /**
     * 查询支行
     * @param bankCode
     * @param text
     */
    private void querySubBankList(String bankCode, String text) {
        HashMap<String, Object> p = new HashMap<String, Object>();
        p.put(BaseProtocolKey.CHANNEL, ClientEngine.getInstance().CHANNEL);
        p.put(BaseProtocolKey.KEY_BANKCODE, String.valueOf(bankCode));
        p.put(BaseProtocolKey.SEARCHTEXT, text);
        p.put(BaseProtocolKey.KEY_PAGESIZE, PAGESIZE);
        bankModel.querySubBankList(p, false);
    }

    /**
     * 提交检查三要素
     */
    private void checkCardThreeElements() {
        final HashMap<String, Object> paramers = new HashMap<String, Object>();
        String phone = AccountUtils.getAccountPhone(_mActivity);
        if (TextUtils.isEmpty(phone)) {
            HPLog.e(HPLog.LFP_TAG, "phone null");
            return;
        }
        String name = releaName.getText().toString();
        if (!CommonUtils.isRealName(name)||name.length()>20) {
            DialogUtils.showToast(_mActivity, getString(R.string.public_name_hint));
            return;
        }
        name = name.trim();
        String mcertNo = certNo.getText().toString();
        try {
            if (CommonUtils.IDCardValidate(mcertNo, true) != null) {
                DialogUtils.showToast(_mActivity, getString(R.string.public_ID_number_hint));
                return;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String sharCode = shareCode.getText().toString();
        if (TextUtils.isEmpty(sharCode)) {
            DialogUtils.showToast(_mActivity, getString(R.string.public_sharecode_hint));
            return;
        }
        mcertNo = mcertNo.toString().trim();
        String cardNo = card_no.getText().toString();
        if (!CommonUtils.isLegalCard(cardNo)) {
            DialogUtils.showToast(_mActivity, getString(R.string.public_bank_Name_hint));
            return;
        }
        //判断主行
        if (TextUtils.isEmpty(bank_name.getText())) {
            DialogUtils.showToast(_mActivity, getString(R.string.select_bank_tip));
            return;
        }
        HPLog.i(HPLog.LFP_TAG, "mMainBank:" + mMainBank.toString());
        //有支行
        if ("1".equals(mMainBank.getSubBankFlag()) && TextUtils.isEmpty(sub_bank.getText())) {
            DialogUtils.showToast(_mActivity, getString(R.string.select_subbank_tip));
            return;
        }
        cardNo = cardNo.trim();
        cardNo = cardNo.replace(" ", "");
        paramers.put(BaseProtocolKey.NAME, SecureManager.getInstance().des(name, 1, null));
        paramers.put(BaseProtocolKey.CARDNO, SecureManager.getInstance().des(cardNo, 1, null));
        paramers.put(BaseProtocolKey.CERTNO, SecureManager.getInstance().des(mcertNo, 1, null));

        paramers.put(BaseProtocolKey.BANKCODE, mMainBank.getBankCode());
        String subBankCode = "";
        if (!TextUtils.isEmpty(sub_bank.getText())) {
            if (bundleBean != null && !TextUtils.isEmpty(bundleBean.getSubBankName()) &&
                    bundleBean.getSubBankName().trim().equalsIgnoreCase(sub_bank.getText().toString().trim())) {
                //这个情况一般是用户没有更改过银行
                subBankCode = bundleBean.getSubBankCode();
            } else {
                //如果更改过银行mSubSelectedBank 必定有值
                subBankCode = mSubSelectedBank.getSubBankCode();
            }
        }
        if (!TextUtils.isEmpty(subBankCode)) {
            paramers.put(BaseProtocolKey.SUBBANKCODE, subBankCode);
        }
        paramers.put(BaseProtocolKey.RECOMMENDER, SecureManager.getInstance().des(sharCode.trim(), 1, null));
        paramers.put(BaseProtocolKey.CLIENTCHANNEL, NoCardPayConfig.CLIENTCHANNEL);
        paramers.put(BaseProtocolKey.CHANNEL, ClientEngine.getInstance().CHANNEL);
        HPLog.i(HPLog.LFP_TAG, "--checkCardThreeElements::params:" + paramers.toString());
        if(isFirstSubmit) {
            DialogUtils.showAlertDialog(_mActivity, _mActivity.getString(R.string.tip), String.format(_mActivity.getString(R.string.hint_confirm_name), releaName.getText().toString()), _mActivity.getString(R.string.dialog_cancel), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            }, _mActivity.getString(R.string.dialog_ok), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    qualificationPresenter.checkCardThreeElements(paramers, true);
                }
            });
        }else {
            qualificationPresenter.checkCardThreeElements(paramers, true);
        }
    }


    @Override
    public void autoQueryCardBin(String cardNo) {
        HPLog.i(HPLog.LFP_TAG, "--焦点失去-发起卡bin查询:" + cardNo);
        //如果输入可之前不一样则清空主行和支行数据
        if (bundleBean != null && !cardNo.equals(bundleBean.getCardNo())) {
            bank_name.setText("");
            sub_bank.setText("");
        }
        if (bankModel != null) {
            HashMap<String, Object> params = new HashMap<>();
            params.put(BaseProtocolKey.CARDNO1, SecureManager.getInstance().des(cardNo, 1, null));
            params.put(BaseProtocolKey.CHANNEL, NoCardPayConfig.CHANNEL);
            params.put(BaseProtocolKey.CLIENTCHANNEL, NoCardPayConfig.CLIENTCHANNEL);
            HPLog.i(HPLog.LFP_TAG, "autoQueryCardBin::params:" + params.toString());
            bankModel.queryBankInfoByCardBin(params, false);
        }
    }

    @Override
    public String getFragmentTitle() {
        return _mActivity.getString(R.string.qualification);
    }

    @Override
    public void showBankList(BankList bankList) {
        this.mBankList = bankList;
        HPLog.i(HPLog.LFP_TAG, "发卡行列表：" + mBankList.getList().size());
        for (BankList.ListBean listBean : bankList.getList()) {
            String bankName = listBean.getBankName();
            if (!bankNames.contains(bankName)) {
                bankNames.add(bankName);
            }
        }
        bankPickerView.setSelectOptions(0);
        bankPickerView.setPicker(bankNames);
        //这里由于网络是异步的
        queryHasSubmitData();
    }

    @Override
    public void queryBankListFail(String baseErrorInfo) {
        DialogUtils.showAlertDialog(_mActivity, _mActivity.getString(R.string.tip), baseErrorInfo, false, _mActivity.getString(R.string.dialog_ok), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
                pop();
            }
        });
    }

    @Override
    public void showSubBankList(SubBankBean subBankBean) {
        sub_bank.refreshBanks(subBankBean.getList());
    }

    @Override
    public void selectedBank(ResultSingleBankBean resultSingleBankBean) {
        HPLog.i(HPLog.LFP_TAG, "--selectedBank::resultSingleBankBean=" + resultSingleBankBean.toString());
        if (resultSingleBankBean != null && !TextUtils.isEmpty(resultSingleBankBean.getBankCode()) && mBankList.getList() != null) {
            BankList.ListBean selectBankInfo = getSelectBankInfo(mBankList.getList(), resultSingleBankBean.getBankCode());
            bank_name.setText(selectBankInfo.getBankName());
        }
        for (BankList.ListBean obj : mBankList.getList()) {
            if (obj.getBankCode().equals(resultSingleBankBean.getBankCode())) {
                mMainBank = obj;
                linearLayoutStatus(obj.getSubBankFlag());
                break;
            }
        }
    }

    private void linearLayoutStatus(String bankSubFlag) {
        if ("1".equals(bankSubFlag)) {
            linear_subbank.setVisibility(View.VISIBLE);
        } else {
            linear_subbank.setVisibility(View.GONE);
        }
    }

    @Override
    public void onBankSelected(SubBankBean.ListBean b) {
        HPLog.i(HPLog.LFP_TAG, "--onBankSelected::" + b.toString());
        // 在用户选中的瞬间，是不用去搜索的。所以先去掉监听，设置完成后，再加入监听。
        if (null != b) {
            this.mSubSelectedBank = b;
            sub_bank.setText(b.getSubBankName());
        } else {
            this.mSubSelectedBank = null;
            sub_bank.setText("");
        }
    }

    @Override
    public void checkCardThreeElementsSuccess(ThreeElementResult response) {
        HPLog.i(HPLog.LFP_TAG, "---checkCardThreeElementsSuccess---" + response.toString());
        if ("1".equals(response.getStatus())) {
            Bundle bundle = new Bundle();
            if (bundleBean == null) {
                bundleBean = new BundleBean();
                bundleBean.setName(releaName.getText().toString().trim());
                bundleBean.setCertNo(certNo.getText().toString().trim());
                bundleBean.setCardNo(card_no.getText().toString().trim().replace(" ", ""));
                bundleBean.setShareCode(shareCode.getText().toString().trim());
                bundleBean.setBankCode(mMainBank.getBankCode());
                bundleBean.setBankName(bank_name.getText().toString().trim());
                if ("1".equals(mMainBank.getSubBankFlag())) {
                    bundleBean.setSubBankCode(mSubSelectedBank.getSubBankCode());
                    bundleBean.setSubBankName(sub_bank.getText().toString().trim());
                }
            } else {
                checkDifferentValue();
            }
            HPLog.i(HPLog.LFP_TAG, "bundleBean::" + bundleBean.toString());
            bundle.putParcelable(BaseProtocolKey.KEY_BASEINFO, bundleBean);
            start(QualificationPictureInfoFragment.newInstance(bundle));
        } else {
            DialogUtils.showAlertDialog(_mActivity, _mActivity.getString(R.string.tip), response.getMessage(), false);
        }
    }

    //合并修改过的值,走到该流程只有是重新提交会进来
    private void checkDifferentValue() {
        //姓名（显示的姓名和从服务器获取的姓名不一致，更新bundleBean中的姓名）
        String tempName = releaName.getText().toString().trim();
        if (!TextUtils.isEmpty(bundleBean.getName()) && !bundleBean.getName().equals(tempName)) {
            bundleBean.setName(tempName);
        }
        //证件
        String tempCertNo = certNo.getText().toString().trim();
        if (!TextUtils.isEmpty(bundleBean.getCertNo()) && !bundleBean.getCertNo().equals(tempCertNo)) {
            bundleBean.setCertNo(tempCertNo);
        }
        //卡号
        String tempCardNo = card_no.getText().toString().trim().replace(" ", "");
        if (!TextUtils.isEmpty(bundleBean.getCardNo()) && !bundleBean.getCardNo().equals(tempCardNo)) {
            bundleBean.setCardNo(tempCardNo);
        }
        //银行卡编号/名称
        String tempBankName = bank_name.getText().toString().trim();
        if (!tempBankName.equals(bundleBean.getBankName())) {
            bundleBean.setBankCode(mMainBank.getBankCode());
            bundleBean.setBankName(mMainBank.getBankName());
        }
        //分行卡名称/编号
        String tempSubBankName = sub_bank.getText().toString().trim();
        if (!TextUtils.isEmpty(tempSubBankName)) {
            if (!tempSubBankName.equals(bundleBean.getSubBankName())) {
                bundleBean.setSubBankCode(mSubSelectedBank.getSubBankCode());
                bundleBean.setSubBankName(mSubSelectedBank.getSubBankName());
            }
        }else{
            bundleBean.setSubBankCode("");
            bundleBean.setSubBankName("");
        }
    }

    @Override
    public void checkCardThreeElementsFail(String baseErrorInfo) {
        HPLog.i(HPLog.LFP_TAG, "---checkCardThreeElementsFail---" + baseErrorInfo);
        DialogUtils.showAlertDialog(_mActivity, getString(R.string.tip), baseErrorInfo, false);
    }

    @Override
    public void queryQualificationInfoSuccess(QualificationQueryBean qualificationQueryBean) {
        HPLog.i(HPLog.LFP_TAG, "queryQualificationInfoSuccess::" + qualificationQueryBean.toString());
        if (qualificationQueryBean != null) {
            AccountUtils.setAccountStatus(_mActivity, AccountUtils.convertStatus(qualificationQueryBean.getStatus()));
            AccountUtils.setAccountFailDes(_mActivity, qualificationQueryBean.getAuditInfo());
            bundleBean = new BundleBean();
            bundleBean.setName(qualificationQueryBean.getName());
            bundleBean.setCertNo(qualificationQueryBean.getCertNo());
            bundleBean.setCardNo(qualificationQueryBean.getCardNum());
            bundleBean.setBankCode(qualificationQueryBean.getBankCode());
            bundleBean.setBankName(qualificationQueryBean.getBankName());
            bundleBean.setSubBankCode(qualificationQueryBean.getSubBankCode());
            bundleBean.setSubBankName(qualificationQueryBean.getBankOfDeposit());
            bundleBean.setFrontCCPicStatus(qualificationQueryBean.getFrontCCPicStatus());
            bundleBean.setFrontCCPic(qualificationQueryBean.getFrontCCPic());
            bundleBean.setBackCCPicStatus(qualificationQueryBean.getBackCCPicStatus());
            bundleBean.setBackCCPic(qualificationQueryBean.getBackCCPic());
            bundleBean.setHoldPicStatus(qualificationQueryBean.getHoldPicStatus());
            bundleBean.setHoldPic(qualificationQueryBean.getHoldPic());
            bundleBean.setShareCode(qualificationQueryBean.getRecommender());
            bundleBean.setAuditInfo(qualificationQueryBean.getAuditInfo());
//            HPLog.i(HPLog.LFP_TAG, "mBankList.getList():" + mBankList.getList().size());
            if (mBankList.getList() != null) {
                for(int i=0;i<mBankList.getList().size();i++){
                    BankList.ListBean listBean= mBankList.getList().get(i);
                    if(listBean!=null&&!TextUtils.isEmpty(listBean.getBankCode())) {
                        if (listBean.getBankCode().trim().equals(bundleBean.getBankCode())) {
                            HPLog.i(HPLog.LFP_TAG, "匹配的数据：" + listBean.toString());
                            mMainBank = listBean;
                            bankPickerView.setSelectOptions(i);
                            break;
                        }
                    }
                }
            }
            fillContent(bundleBean);
        }
    }

    @Override
    public void queryFail(String failMessage) {
        DialogUtils.showAlertDialog(_mActivity, _mActivity.getString(R.string.tip), failMessage, false, _mActivity.getString(R.string.dialog_ok), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                pop();
            }
        });
    }

    @Override
    public void onStop() {
        super.onStop();
        sub_bank.onStop();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==TAKE_PHOTO) {
            if (resultCode == RESULT_OK) {
                String qrCode = data.getStringExtra("result");
                String tempCode = null;
                Map<String, String> parserResult = CommonHelper.urlSplit(qrCode);
                HPLog.i(HPLog.LFP_TAG, parserResult.toString());
                if (parserResult.containsKey("recommender")) {
                    tempCode = parserResult.get("recommender");
                    shareCode.setText(tempCode);
                } else {
                    scannerQrCodeError();
                }
            }
        }
    }

    //如果扫描二维码没有内容或者没有推荐吗提示手动输入
    private void scannerQrCodeError() {
        DialogUtils.showAlertDialog(_mActivity, _mActivity.getString(R.string.tip), _mActivity.getString(R.string.hint_scanner_error), true, _mActivity.getString(R.string.dialog_ok), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
    }

    /**
     * 获取当选取的开户行信息
     *
     * @param beanList
     * @param bankCode
     * @return
     */
    public BankList.ListBean getSelectBankInfo(List<BankList.ListBean> beanList, String bankCode) {
        BankList.ListBean result = null;
        if (beanList.isEmpty() || TextUtils.isEmpty(bankCode)) {
            return null;
        }
        for(int i=0;i<beanList.size();i++){
            if (bankCode.equals(beanList.get(i).getBankCode())) {
                result = beanList.get(i);
                //自动选择到相应的item
                bankPickerView.setSelectOptions(i);
                break;
            }
        }
        return result;
    }


    @Override
    public void permissionYes(List<String> permissions) {
        //going on
        Intent intent = new Intent(_mActivity, CaptureActivity.class);
        intent.putExtra("paperFromQualificationFragmentFlag",true);
        startActivityForResult(intent, TAKE_PHOTO);
    }

    @Override
    public void permissionFail(List<String> permissions) {
        // 用户否勾选了不再提示并且拒绝了权限，那么提示用户到设置中授权。
        if (AndPermission.hasAlwaysDeniedPermission(_mActivity, permissions)) {
            // 第一种：用默认的提示语。
            AndPermission.defaultSettingDialog(QualificationBaseInfoFragment.this, PermissionCode.REQUEST_CODE_SETTING).show();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        hideSoftInput();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        qualificationPresenter.onDestory();
        permissonRequest = null;
    }
}
